﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.DAO;
using BidFunLib.Entity;

namespace BidFunLib.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class BidObjectManager
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(BidObjectManager));

        /// <summary>
        /// The dao of this manager
        /// </summary>
        private BidObjectDAO dao = new BidObjectDAO();

        /// <summary>
        /// Singleton instance
        /// </summary>
        private static BidObjectManager instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static BidObjectManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BidObjectManager();
                }
                return BidObjectManager.instance;
            }
            set
            {
                BidObjectManager.instance = value;
            }
        }

        private BidObjectManager()
        {
            ListObject = new List<BidObject>();
        }

        /// <summary>
        /// 
        /// </summary>
        public List<BidObject> ListObject { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        public BidObject getObjectByLabel(String label)
        {
            // search in memory list
            foreach (BidObject bidObject in ListObject)
            {
                if (bidObject.Label == label)
                {
                    log.Debug("User " + bidObject.Label + " already exist in memory.");
                    return bidObject;
                }
            }

            // not found so search in database or create if not exist
            BidObject newBidObject = retreiveOrCreateBidObjectByLabel(label);
            ListObject.Add(newBidObject);
            return newBidObject;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bid"></param>
        public void UpdateBidObject(BidObject bid)
        {
            dao.updateBidObject(bid);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        private BidObject retreiveOrCreateBidObjectByLabel(String label)
        {
            BidObject bidObject = null;

            // first of all we search for pseudo in DB
            bidObject = dao.getBidObjectByLabel(label);

            // not found so we save a new one
            if (bidObject == null)
            {
                log.Debug("Object " + label + " must be created and saved into database.");
                bidObject = new BidObject();
                bidObject.Label = label;

                dao.saveBidObject(bidObject);

                bidObject = dao.getBidObjectByLabel(label);
            }
            return bidObject;
        }



        public BidObject getObjectById(Int32 objectId)
        {
            BidObject bidObject = null;
            bidObject = dao.getObjectById(objectId);
            return bidObject;
        }
    }
}
